package com.dd.cloud.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dd.cloud.auth.contants.RoleConstants;
import com.dd.cloud.auth.entity.RoleMenu;
import com.dd.cloud.auth.entity.UserToRole;
import com.dd.cloud.auth.mapper.UserToRoleMapper;
import com.dd.cloud.auth.res.UserToRoleRes;
import com.dd.cloud.auth.service.IAdminPowerService;
import com.dd.cloud.auth.service.IRoleMenuService;
import com.dd.cloud.auth.service.IUserToRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-06-01
 */
@Service
public class UserToRoleServiceImpl extends ServiceImpl<UserToRoleMapper, UserToRole> implements IUserToRoleService {

    @Autowired
    private IRoleMenuService roleMenuService;
    @Autowired
    private IAdminPowerService adminPowerService;

    /**
     * @description 获取权限id 通过用户id和用户类型
     * @param loginInfo
     * @return Integer 权限id
     * @author czg
     * @date 2023/6/5 11:21
    */
    @Override
    public Integer getRoleIdByUser(LoginInfo loginInfo) {
        QueryWrapper<UserToRole> qw = new QueryWrapper<>();
        qw.lambda().eq(UserToRole::getUserId, loginInfo.getId())
                .eq(UserToRole::getUserType, loginInfo.getType())
                .eq(UserToRole::getStatus, RoleConstants.UserToRoleStatus.NOMER);
        UserToRole userToRole = getOne(qw);
        if (userToRole != null){
            loginInfo.setRoleId(userToRole.getRoleId());
            // 2、然后将查询到的LoginUser对象的相关信息封装到UsernamePasswordAuthenticationToken对象中，然后将该对象存储到Security的上下文对象中
            //UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginInfo, null , null) ;
            //SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            return userToRole.getRoleId();
        }

        return 0;
    }

    /**
     * @description 验证用户请求的url是否有权限
     * @param uri: 请求路径
     * @return Boolean
     * @author czg
     * @date 2023/6/5 18:02
    */
    @Override
    public Boolean checkRoleToUrl(String uri) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Integer id = getRoleIdByUser(loginInfo);
        List<RoleMenu> roleMenus = roleMenuService.queryMenuByUserId(id);
        if (roleMenus == null || roleMenus.isEmpty()){
            return false;
        }
        List<String> strings = roleMenus.stream().map(item -> {
            return item.getServerUrl();
        }).collect(Collectors.toList());
        if (strings.contains(uri)){
            return true;
        }
        return false;
    }

    /**
     * @description 查询所有用户及权限
     * @param :
     * @return List<UserToRoleRes>
     * @author czg
     * @date 2023/6/12 17:33
    */
    @Override
    public List<UserToRoleRes> getUserRoleList() {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Integer userId = null;
        if (loginInfo.getType() == LoginInfo.UserType.PHAR){
            userId = loginInfo.getPharId();
        }
        List<UserToRoleRes> list = getBaseMapper().getUserRoleList(userId, loginInfo.getType());
        return list;
    }
}
